

from textual.app import ComposeResult
from textual.binding import Binding
from textual.containers import Center, Vertical, VerticalScroll
from textual.screen import ModalScreen
from textual.widgets import Markdown, Button, Footer
from typing_extensions import Final

from pathlib import Path

from .. import __version__
from ..utility.constants import APPLICATION_TITLE

HELP: Final[str] = f"""\
# {APPLICATION_TITLE} v{__version__} - 帮助界面
""" + (Path(__file__).parent.parent.parent / "help.md").read_text()

class HelpDialog(ModalScreen[None]):

    DEFAULT_CSS = """
    HelpDialog {
        align: center middle;
    }

    HelpDialog > Vertical {
        border: thick $primary 50%;
        width: 80%;
        height: 80%;
        background: $boost;
    }

    HelpDialog > Vertical > VerticalScroll {
        height: 1fr;
        margin: 1 2;
    }

    HelpDialog > Vertical > Center {
        padding: 1;
        height: auto;
    }
    """

    BINDINGS = [
        Binding("escape", "dismiss", "", show=False),
    ]

    def compose(self) -> ComposeResult:
        with Vertical():
            with VerticalScroll():
                yield Markdown(HELP)
            with Center():
                yield Button('Close', variant='primary')

    def on_mount(self) -> None:
        self.query_one(Button).focus()

    def on_button_pressed(self) -> None:
        self.dismiss(None)

    def on_markdown_link_clicked(self, event: Markdown.LinkClicked) -> None:
        self.app.open_url(event.href)